package com.example.demo.service;

import com.example.demo.main.Admin;
import com.example.demo.main.AdminExample;
import com.example.demo.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminExample adminExample;

    public Integer insertByClass(Admin admin) {
        return adminMapper.insertSelective(admin);
    }

    public Boolean checkPwd(Admin admin) {
        adminExample.clear();
        adminExample.or().andAdminNameEqualTo(admin.getAdminName()).andAdminPwdEqualTo(admin.getAdminPwd());
        return adminMapper.selectByExample(adminExample).size() > 0;
    }

    public String getPower(String adminName) {
        if (adminMapper.selectByPrimaryKey(adminName).getPower() > 1)
            return "初级管理员";
        else return "高级管理员";
    }

    public Boolean isHPower(String adminName) {
        return getPower(adminName).equals("高级管理员");
    }

    public Boolean updateByName(String val, String str, String name) {
        Admin admin = new Admin();
        admin.setAdminName(name);
        switch (str) {
            case "power":
                admin.setPower(Integer.parseInt(val));
                break;
            case "email":
                admin.setEmail(val);
                break;
            case "phone":
                admin.setPhone(val);
                break;
        }
        return adminMapper.updateByPrimaryKeySelective(admin) > 0;
    }
}
